package edu.zut.bailan.student.service.impl;


import edu.zut.bailan.base.service.impl.GenericManagerImpl;
import edu.zut.bailan.student.dao.StudentsDao;
import edu.zut.bailan.student.domain.Student;
import edu.zut.bailan.student.service.StudentManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentManagerImpl extends GenericManagerImpl<Student, Long>
        implements StudentManager {

    StudentsDao studentsDao;

    @Autowired
    public void setStudentDao(StudentsDao studentsDao) {
        this.studentsDao = studentsDao;
        this.dao = this.studentsDao;
    }

    @Override
    public List<Student> findByName(String name) {
        // 创建查询条件数据对象
        Student queryObject = new Student();
        queryObject.setName(name);
        // 创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() // 构建对象
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.startsWith()) // 姓名采用“开始匹配”的方式查询
                .withIgnorePaths("dateCreated", "dateModified"); // 忽略属性：是否关注。因为是基本类型，需要忽略掉
        // 创建实例并查询
        Example<Student> ex = Example.of(queryObject, matcher);
        List<Student> result = dao.findAll(ex);
        return result;
    }

    @Override
    public List<Student> findByPostcode(String postcode) {

        // 创建查询条件数据对象
        Student queryObject = new Student();
        queryObject.setDateCreated(null);
        queryObject.setDateModified(null);
        queryObject.setPostcode(postcode);
        // 创建匹配器，即如何使用查询条件
        // 创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() // 构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true) // 改变默认大小写忽略方式：忽略大小写
                .withMatcher("postcode", ExampleMatcher.GenericPropertyMatchers.startsWith()); // 地址采用“开始匹配”的方式查询
        // 创建实例并查询
        Example<Student> ex = Example.of(queryObject, matcher);
        List<Student> result = dao.findAll(ex);
        return result;
    }

}